
#include <assert.h>
#include "app_platform/cmd_obj.h"
#include "app_platform/cntl_info.h"
#include "app_platform/intf_service_control.h"
//#include "logservice/logger.h"
#include "giscoreao_adaptor.h"
#include "giscoreao_req_resp.h"


std::string g_sgiscoreaoStandAlone;

///////////////////////
// class CGisCoreAoSvcFactory
int CGisCoreAoSvcFactory::init(void* pvParam)
{
	IUserDefineCntl* pCntl = reinterpret_cast<IUserDefineCntl*>(pvParam);
	assert(pCntl);
    pCntl->getConfig("StandAlone", g_sgiscoreaoStandAlone);
         

	return CGisCoreAo::Initialize(pCntl);
}

int CGisCoreAoSvcFactory::CXYZTimerHandler::OnTimer(int iTimerId)
{
    assert(0);
	return 0;
}

int CGisCoreAoSvcFactory::fini(void* pvParam)
{
    return 0;
}

uint32_t CGisCoreAoSvcFactory::getCateId() const
{   
	//方法已废弃
    return 0;
}

IService* CGisCoreAoSvcFactory::createService(uint32_t dwcmdId)
{
    return (new CGisCoreAoService(dwcmdId));
}

void CGisCoreAoSvcFactory::getServiceType(ServiceTypeMap_T& mapSvcType)
{
	mapSvcType[GISCOREAO_CHECKAREACOVER_REQ_CMD_ID] = true;
	mapSvcType[GISCOREAO_GETMERGECOORDINATE_REQ_CMD_ID] = true;
}

////////////////////////
// class CGisCoreAoService
CGisCoreAoService::CGisCoreAoService(uint32_t dwcmdId) 
: m_dwcmdId(dwcmdId)
{
    enum DeployMethod method = DEPLOY_DETACH;
    if(!g_sgiscoreaoStandAlone.empty() && g_sgiscoreaoStandAlone != "0") // Stand-Alone
        method = DEPLOY_STANDALONE;
        
    m_pSvc = new CGisCoreAo(method);
}

void CGisCoreAoService::Reset() 
{ 
	m_pSvc -> Reset();
}
    
void CGisCoreAoService::getCmdIdRange(uint32_t& dwcmdIdBegin, uint32_t& dwcmdIdEnd) const 
{ 
    dwcmdIdBegin = m_dwcmdId;
    dwcmdIdEnd = m_dwcmdId;
} 
	
int CGisCoreAoService::Execute(CCmdObj& oCmd)
{
	switch(oCmd.getCmdId())
	{
	case GISCOREAO_CHECKAREACOVER_REQ_CMD_ID:
        return CheckAreaCover(oCmd);
	case GISCOREAO_GETMERGECOORDINATE_REQ_CMD_ID:
        return GetMergeCoordinate(oCmd);
	default:
		assert(0);
	}
	return 0;
}

int CGisCoreAoService::CheckAreaCover(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisCoreAoCheckAreaCoverReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	CGisCoreAoCheckAreaCoverResp oResp;
	
    oResp.dwResult = m_pSvc->CheckAreaCover(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.shipTypeAreaGisCheckPo,
		oReq.inReserve,
		oReq.CheckResult,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.CheckResult = oReq.CheckResult;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


int CGisCoreAoService::GetMergeCoordinate(CCmdObj& oCmd)
{
	char* pBuf = NULL; uint32_t nLen = 0;
    oCmd.getBuffer(pBuf, nLen);
    CByteStreamNetwork bs(pBuf, nLen);
    bs.isStoring(false);
    bs.setVersion(oCmd.getVersion());
    CGisCoreAoGetMergeCoordinateReq oReq;
    if(!oReq.Serialize(bs))
    {
        m_sLastErrMsg = "Req.Serialize(bs) failed.";
        return -1;
    }

    CCntlInfo oCntlInfo;
    oCntlInfo.setCntl(reinterpret_cast<void*>(m_pSvcCntl));
    oCntlInfo.setOperatorUin(oCmd.getOperatorUin());
	oCntlInfo.setOperatorClientIP(oCmd.getOperatorClientIP());
	oCntlInfo.setOperatorKey(oCmd.getOperatorKey());
    oCntlInfo.setRouteKey(oCmd.getUin());
    oCntlInfo.setCmdId(oCmd.getCmdId());
    oCntlInfo.setNetioClientIP(oCmd.getNetioClientIP());
	oCntlInfo.setSerialNo(oCmd.getSerialNo());
	oCntlInfo.setPkgTimestamp(oCmd.getPkgTimestamp());
	
	CGisCoreAoGetMergeCoordinateResp oResp;
	
    oResp.dwResult = m_pSvc->GetMergeCoordinate(
		oCntlInfo,
		oReq.machineKey,
		oReq.source,
		oReq.sceneId,
		oReq.destAddrProvince,
		oReq.destAddrCity,
		oReq.destAddrDistrict,
		oReq.destAddrText,
		oReq.inReserve,
		oReq.mergedCoordinatesPo,
		oReq.errMsg,
		oReq.outReserve
    );
	oResp.mergedCoordinatesPo = oReq.mergedCoordinatesPo;
	oResp.errMsg = oReq.errMsg;
	oResp.outReserve = oReq.outReserve;

    return m_pSvcCntl->putSvcObj(&oResp, oResp.dwResult);
}


#include "svc_main.h"

IServiceFactory* GetServiceFactory(void)
{
    return CSvcFactorySingleton::Instance();
}
